package com.ys.timer;

import com.ys.service.business.TpAmazonShopProductCountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
public class TimerTPAmazonShopProductCountDaily {
	private static final Logger logger = LoggerFactory.getLogger(TimerTPAmazonShopProductCountDaily.class);

	@Autowired
	private TpAmazonShopProductCountService tpAmazonShopProductCountService;



	//每天3点执行，统计前一日数据
	//@Scheduled(cron = "0 0 3 * * * ")
	//开发测试时间间隔30秒执行一次
	//@Scheduled(cron = "*/30 * * * * ?")
	//@Scheduled(cron = "5 * * * * * ")
	public void addTpAmazonShopOrderDaily() {
		try {
			logger.info("***********定时统计运营跟踪表数据开始***********");
			//删除30天之前的统计数据
			tpAmazonShopProductCountService.deleteAmazonShopProductCount();
			//新增
			tpAmazonShopProductCountService.addTpAmazonShopOrderDaily();
			logger.info("***********定时统计运营跟踪表数据完成***********");
		}catch (Exception e){
			//e.printStackTrace();
			logger.error("定时统计运营跟踪表数据出错：",e.getMessage(),e);
		}
	}


	//每天4点执行，修改5天前的（SessionsNum会话次数 ；RecommendPrice推荐报价百分比；CommoditySession商品会话百分比；SalesRank销售排名 ）几个字段
	//@Scheduled(cron = "0 0 4 * * * ")
	//开发测试时间间隔30秒执行一次
	//@Scheduled(cron = "*/30 * * * * ?")
	//@Scheduled(cron = "5 * * * * * ")
	public void updateTpAmazonShopOrderDaily() {
		try {
			logger.info("***********定时修改运营跟踪表前5天数据开始***********");
			//删除30天之前的统计数据
			tpAmazonShopProductCountService.deleteAmazonShopProductCount();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for(int i=1;i<=5;i++){
				//获取5天前时间日期
				Calendar calendar = Calendar.getInstance();
				calendar.add(Calendar.DAY_OF_MONTH, -i);
				String agoDay = sdf.format(calendar.getTime());
				logger.info("*************刷新运营跟踪表:"+agoDay+" 记录*************");
				tpAmazonShopProductCountService.updateTpAmazonShopOrderDailyByDay(agoDay);
			}
			logger.info("***********定时修改运营跟踪表前5天数据完成***********");
		}catch (Exception e){
			logger.error("定时修改运营跟踪表前3天数据出错：",e.getMessage(),e);
		}

	}


	
}
